﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Runtime.InteropServices;
using UnityEngine.Events;

public class GDTADAgent : MonoBehaviour
{
    public static GDTADAgent Instance
    {
        get; private set;
    }

    /// <summary>
    /// true: 有激励视频, false: 没有
    /// </summary>
    bool isHasRewardVideo = false;

    [RuntimeInitializeOnLoadMethod]
    private static void Init()
    {
        GameObject obj = new GameObject("GDTAgent");
        Instance = obj.AddComponent<GDTADAgent>();
        DontDestroyOnLoad(obj);
    }

#if UNITY_IPHONE
    // 插屏
    [DllImport("__Internal")]
    static extern void _LoadTXInterstitialAD();
    [DllImport("__Internal")]
    static extern void _ShowTXInterstitialAD();
    [DllImport("__Internal")]
    static extern void _InitTXInterstitial(string pszAppID, string pszUtilID);

    // 激励
    [DllImport("__Internal")]
    static extern void _LoadTXRewardVideoAD();
    [DllImport("__Internal")]
    static extern void _ShowTXRewardVideoAD();
    [DllImport("__Internal")]
    static extern void _IsHasRewardVideo();
    [DllImport("__Internal")]
    static extern void _InitTXRewardVideo(string pszAppID, string pszUtilID);
#endif

    // 插屏回调
    public UnityAction<string> funUnifiedInterstitialSuccessToLoadAd;
    public UnityAction<string> funUnifiedInterstitialFailToLoadAd;
    public UnityAction<string> funUnifiedInterstitialWillLeaveApplication;
    public UnityAction<string> funUnifiedInterstitialWillExposure;
    public UnityAction<string> funUnifiedInterstitialClicked;
    public UnityAction<string> funUnifiedInterstitialAd;

    public UnityAction<string> funUnifiedInterstitialWillPresentScreen;
    public UnityAction<string> funUnifiedInterstitialDidPresentScreen;
    public UnityAction<string> funUnifiedInterstitialFailToPresent;
    public UnityAction<string> funUnifiedInterstitialDidDismissScreen;

    public UnityAction<string> funUnifiedInterstitialAdWillPresentFullScreenModal;
    public UnityAction<string> funUnifiedInterstitialAdDidPresentFullScreenModal;
    public UnityAction<string> funUnifiedInterstitialAdWillDismissFullScreenModal;
    public UnityAction<string> funUnifiedInterstitialAdDidDismissFullScreenModal;

    public UnityAction<string> funUnifiedInterstitialAdViewWillPresentVideoVC;
    public UnityAction<string> funUnifiedInterstitialAdViewDidPresentVideoVC;
    public UnityAction<string> funUnifiedInterstitialAdViewWillDismissVideoVC;
    public UnityAction<string> funUnifiedInterstitialAdViewDidDismissVideoVC;

    // 激励回调
    public UnityAction<string> funGdt_rewardVideoAdVideoDidLoad;
    public UnityAction<string> funGdt_rewardVideoAdDidLoad;
    public UnityAction<string> funGdt_rewardVideoAdWillVisible;
    public UnityAction<string> funGdt_rewardVideoAdDidExposed;
    public UnityAction<string> funGdt_rewardVideoAdDidClose;
    public UnityAction<string> funGdt_rewardVideoAdDidClicked;
    public UnityAction<string> funGdt_rewardVideoAd;
    public UnityAction<string> funGdt_rewardVideoAdDidRewardEffective;
    public UnityAction<string> funGdt_rewardVideoAdDidPlayFinish;
    public UnityAction<string> funGIsHasRewardVideo;

    /// <summary>
    /// 初始化广告SDK
    /// </summary>
    /// <param name="strAppID">应用ID</param>
    /// <param name="strInterstitialID">插屏ID</param>
    /// <param name="strRewardVideoID">激励ID</param>
    public void InitSDK(string strAppID
        , string strInterstitialID
        , string strRewardVideoID)
    {
        Debug.Log("GDTAgent::InitSDK(): " + string.Format("appid:{0}, interstitial:{1}, rewardVideo:{2}",
            strAppID, strInterstitialID, strRewardVideoID));

        if (Application.platform == RuntimePlatform.IPhonePlayer)
        {
            InitInterstitial(strAppID, strInterstitialID);
            InitRewardVideo(strAppID, strRewardVideoID);
        }
    }

    #region 插屏方法
    /// <summary>
    /// 初始化插屏
    /// </summary>
    /// <param name="strAppID"></param>
    /// <param name="strUtilID"></param>
    void InitInterstitial(string strAppID, string strUtilID)
    {
#if UNITY_IPHONE
            _InitTXInterstitial(strAppID, strUtilID);
#endif
    }
    /// <summary>
    /// 加载插屏广告
    /// </summary>
    public void LoadInterstitialAD()
    {
#if UNITY_IPHONE
            _LoadTXInterstitialAD();
#endif
    }
    /// <summary>
    /// 显示插屏广告
    /// </summary>
    public void ShowInterstitialAD()
    {
#if UNITY_IPHONE
            _ShowTXInterstitialAD();
#endif
    }
    #endregion

    #region 激励方法
    void InitRewardVideo(string strAppID, string strUtilID)
    {
#if UNITY_IPHONE
            _InitTXRewardVideo(strAppID, strUtilID);
#endif
    }
    public void LoadRewardVideoAD()
    {
#if UNITY_IPHONE
            _LoadTXRewardVideoAD();
#endif
    }
    public void ShowRewardVideoAD()
    {
#if UNITY_IPHONE
            _ShowTXRewardVideoAD();
#endif
    }
    #endregion

    #region 插屏回调
    /**
     *  插屏2.0广告预加载成功回调
     *  当接收服务器返回的广告数据成功且预加载后调用该函数
     */
    public void unifiedInterstitialSuccessToLoadAd(string strEmpty)
    {
        Debug.Log("GDTAgent::unifiedInterstitialSuccessToLoadAd()" + strEmpty);
        funUnifiedInterstitialSuccessToLoadAd?.Invoke(strEmpty);
    }

    /**
     *  插屏2.0广告预加载失败回调
     *  当接收服务器返回的广告数据失败后调用该函数
     */
    public void unifiedInterstitialFailToLoadAd(string strError)
    {
        Debug.Log("GDTAgent::unifiedInterstitialFailToLoadAd()" + strError);
        funUnifiedInterstitialFailToLoadAd?.Invoke(strError);
    }

    /**
     *  插屏2.0广告将要展示回调
     *  插屏2.0广告即将展示回调该函数
     */
    public void unifiedInterstitialWillPresentScreen(string strEmpty)
    {
        Debug.Log("GDTAgent::unifiedInterstitialWillPresentScreen()" + strEmpty);
        funUnifiedInterstitialWillPresentScreen?.Invoke(strEmpty);
    }

    /**
     *  插屏2.0广告视图展示成功回调
     *  插屏2.0广告展示成功回调该函数
     */
    public void unifiedInterstitialDidPresentScreen(string strEmpty)
    {
        Debug.Log("GDTAgent::unifiedInterstitialDidPresentScreen()" + strEmpty);
        funUnifiedInterstitialDidPresentScreen?.Invoke(strEmpty);
    }

    /**
     *  插屏2.0广告视图展示失败回调
     *  插屏2.0广告展示失败回调该函数
     */
    public void unifiedInterstitialFailToPresent(string strError)
    {
        Debug.Log("GDTAgent::unifiedInterstitialFailToPresent()" + strError);
        funUnifiedInterstitialFailToPresent?.Invoke(strError);
    }

    /**
     *  插屏2.0广告展示结束回调
     *  插屏2.0广告展示结束回调该函数
     */
    public void unifiedInterstitialDidDismissScreen(string strEmpty)
    {
        Debug.Log("GDTAgent::unifiedInterstitialDidDismissScreen()" + strEmpty);
        funUnifiedInterstitialDidDismissScreen?.Invoke(strEmpty);
    }
    /**
     *  当点击下载应用时会调用系统程序打开其它App或者Appstore时回调
     */
    public void unifiedInterstitialWillLeaveApplication(string strEmpty)
    {
        Debug.Log("GDTAgent::unifiedInterstitialWillLeaveApplication()" + strEmpty);
        funUnifiedInterstitialWillLeaveApplication?.Invoke(strEmpty);
    }
    /**
     *  插屏2.0广告曝光回调
     */
    public void unifiedInterstitialWillExposure(string strEmpty)
    {
        Debug.Log("GDTAgent::unifiedInterstitialWillExposure()" + strEmpty);
        funUnifiedInterstitialWillExposure?.Invoke(strEmpty);
    }
    /**
     *  插屏2.0广告点击回调
     */
    public void unifiedInterstitialClicked(string strEmpty)
    {
        Debug.Log("GDTAgent::unifiedInterstitialClicked()" + strEmpty);
        funUnifiedInterstitialClicked?.Invoke(strEmpty);
    }
    /**
     *  点击插屏2.0广告以后即将弹出全屏广告页
     */
    public void unifiedInterstitialAdWillPresentFullScreenModal(string strEmpty)
    {
        Debug.Log("GDTAgent::unifiedInterstitialAdWillPresentFullScreenModal()" + strEmpty);
        funUnifiedInterstitialAdWillPresentFullScreenModal?.Invoke(strEmpty);
    }
    /**
     *  点击插屏2.0广告以后弹出全屏广告页
     */
    public void unifiedInterstitialAdDidPresentFullScreenModal(string strEmpty)
    {
        Debug.Log("GDTAgent::unifiedInterstitialAdDidPresentFullScreenModal()" + strEmpty);
        funUnifiedInterstitialAdDidPresentFullScreenModal?.Invoke(strEmpty);
    }

    /**
     *  全屏广告页将要关闭
     */
    public void unifiedInterstitialAdWillDismissFullScreenModal(string strEmpty)
    {
        Debug.Log("GDTAgent::unifiedInterstitialAdWillDismissFullScreenModal()" + strEmpty);
        funUnifiedInterstitialAdWillDismissFullScreenModal?.Invoke(strEmpty);
    }

    /**
     *  全屏广告页被关闭
     */
    public void unifiedInterstitialAdDidDismissFullScreenModal(string strEmpty)
    {
        Debug.Log("GDTAgent::unifiedInterstitialAdDidDismissFullScreenModal()" + strEmpty);
        funUnifiedInterstitialAdDidDismissFullScreenModal?.Invoke(strEmpty);
    }

    /**
     * 插屏2.0视频广告 player 播放状态更新回调
     */
    public void unifiedInterstitialAd(string strEmpty)
    {
        Debug.Log("GDTAgent::unifiedInterstitialAd()" + strEmpty);
        funUnifiedInterstitialAd?.Invoke(strEmpty);
    }

    /**
     * 插屏2.0视频广告详情页 WillPresent 回调
     */
    public void unifiedInterstitialAdViewWillPresentVideoVC(string strEmpty)
    {
        Debug.Log("GDTAgent::unifiedInterstitialAdViewWillPresentVideoVC()" + strEmpty);
        funUnifiedInterstitialAdViewWillPresentVideoVC?.Invoke(strEmpty);
    }

    /**
     * 插屏2.0视频广告详情页 DidPresent 回调
     */
    public void unifiedInterstitialAdViewDidPresentVideoVC(string strEmpty)
    {
        Debug.Log("GDTAgent::unifiedInterstitialAdViewDidPresentVideoVC()" + strEmpty);
        funUnifiedInterstitialAdViewDidPresentVideoVC?.Invoke(strEmpty);
    }

    /**
     * 插屏2.0视频广告详情页 WillDismiss 回调
     */
    public void unifiedInterstitialAdViewWillDismissVideoVC(string strEmpty)
    {
        Debug.Log("GDTAgent::unifiedInterstitialAdViewWillDismissVideoVC()" + strEmpty);
        funUnifiedInterstitialAdViewWillDismissVideoVC?.Invoke(strEmpty);
    }

    /**
     * 插屏2.0视频广告详情页 DidDismiss 回调
     */
    public void unifiedInterstitialAdViewDidDismissVideoVC(string strEmpty)
    {
        Debug.Log("GDTAgent::unifiedInterstitialAdViewDidDismissVideoVC()" + strEmpty);
        funUnifiedInterstitialAdViewDidDismissVideoVC?.Invoke(strEmpty);
    }
    #endregion

    #region 激励视频回调
    public void gdt_rewardVideoAdVideoDidLoad(string strEmpty)
    {
        Debug.Log("GDTAgent::gdt_rewardVideoAdVideoDidLoad()" + strEmpty);
        isHasRewardVideo = true;
        funGdt_rewardVideoAdVideoDidLoad?.Invoke(strEmpty);
    }
    /**
     广告数据加载成功回调

     @param rewardedVideoAd GDTRewardVideoAd 实例
     */
    public void gdt_rewardVideoAdDidLoad(string strEmpty)
    {
        Debug.Log("GDTAgent::gdt_rewardVideoAdDidLoad()" + strEmpty);
        funGdt_rewardVideoAdVideoDidLoad?.Invoke(strEmpty);
    }

    /**
     视频播放页即将展示回调

     @param rewardedVideoAd GDTRewardVideoAd 实例
     */
    public void gdt_rewardVideoAdWillVisible(string strEmpty)
    {
        Debug.Log("GDTAgent::gdt_rewardVideoAdWillVisible()" + strEmpty);
        funGdt_rewardVideoAdWillVisible?.Invoke(strEmpty);
        isHasRewardVideo = false;
    }

    /**
     视频广告曝光回调

     @param rewardedVideoAd GDTRewardVideoAd 实例
     */
    public void gdt_rewardVideoAdDidExposed(string strEmpty)
    {
        Debug.Log("GDTAgent::gdt_rewardVideoAdDidExposed()" + strEmpty);
        funGdt_rewardVideoAdDidExposed?.Invoke(strEmpty);
    }
    /**
     视频播放页关闭回调

     @param rewardedVideoAd GDTRewardVideoAd 实例
     */
    public void gdt_rewardVideoAdDidClose(string strEmpty)
    {
        Debug.Log("GDTAgent::gdt_rewardVideoAdDidClose()" + strEmpty);
        funGdt_rewardVideoAdDidClose?.Invoke(strEmpty);
    }

    /**
     视频广告信息点击回调

     @param rewardedVideoAd GDTRewardVideoAd 实例
     */
    public void gdt_rewardVideoAdDidClicked(string strEmpty)
    {
        Debug.Log("GDTAgent::gdt_rewardVideoAdDidClicked()" + strEmpty);
        funGdt_rewardVideoAdDidClicked?.Invoke(strEmpty);
    }

    /**
     视频广告各种错误信息回调

     @param rewardedVideoAd GDTRewardVideoAd 实例
     @param error 具体错误信息
     */
    public void gdt_rewardVideoAd(string strEmpty)
    {
        Debug.Log("GDTAgent::gdt_rewardVideoAd()" + strEmpty);
        funGdt_rewardVideoAd?.Invoke(strEmpty);
    }

    /**
     视频广告播放达到激励条件回调

     @param rewardedVideoAd GDTRewardVideoAd 实例
     */
    public void gdt_rewardVideoAdDidRewardEffective(string strEmpty)
    {
        Debug.Log("GDTAgent::gdt_rewardVideoAdDidRewardEffective()" + strEmpty);
        funGdt_rewardVideoAdDidRewardEffective?.Invoke(strEmpty);
    }

    /**
     视频广告视频播放完成

     @param rewardedVideoAd GDTRewardVideoAd 实例
     */
    public void gdt_rewardVideoAdDidPlayFinish(string strEmpty)
    {
        Debug.Log("GDTAgent::gdt_rewardVideoAdDidPlayFinish()" + strEmpty);
        funGdt_rewardVideoAdDidPlayFinish?.Invoke(strEmpty);
    }

    /// <summary>
    /// true:有激励广告, false: 没有激励广告
    /// </summary>
    public bool IsHaveRewardVideo
    {
        get { return isHasRewardVideo; }
    }

    /// <summary>
    /// object-c调用
    /// </summary>
    /// <param name="strRet"></param>
    public void IsHasRewardVideo(string strRet)
    {
        Debug.Log("GDTAgent::IsHasRewardVideo()" + strRet);
        funGIsHasRewardVideo?.Invoke(strRet);
    }
    #endregion
}
